package com.kujie.access.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kujie.access.domain.po.AccessCustomerServer;
import com.kujie.access.domain.query.CustomerServerQuery;
import com.kujie.access.domain.vo.CustomerServerVO;
import com.kujie.access.mapper.AccessCustomerServerMapper;
import com.kujie.access.service.ICustomerServerService;
import com.kujie.common.core.utils.StringUtils;
import com.kujie.common.excel.utils.ExcelUtil;
import com.kujie.common.mybatis.core.page.PageQuery;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 客户服务管理 impl
 *
 * @author check
 * @date 2025-06-30
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CustomerServerServiceImpl implements ICustomerServerService {
    private final AccessCustomerServerMapper accessCustomerServerMapper;

    @Override
    public Page<CustomerServerVO> list(CustomerServerQuery customerServerQuery, PageQuery pageQuery) {
        LambdaQueryWrapper<AccessCustomerServer> accessCustomerServerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildLambdaQueryWrapper(customerServerQuery, accessCustomerServerLambdaQueryWrapper);

        Page<CustomerServerVO> result = pageQuery.build(new CustomerServerVO());

        Page<AccessCustomerServer> accessCustomerServerPage = accessCustomerServerMapper.selectPage(pageQuery.build(), accessCustomerServerLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(accessCustomerServerPage.getRecords())) {
            log.info("数据为空");
            return result;
        }

        List<CustomerServerVO> voList = accessCustomerServerPage.getRecords().stream().map(e -> {
            CustomerServerVO customerServerVO = new CustomerServerVO();
            BeanUtils.copyProperties(e, customerServerVO);
            return customerServerVO;
        }).toList();

        result.setTotal(accessCustomerServerPage.getTotal());
        result.setRecords(voList);
        return result;
    }

    @Override
    public CustomerServerVO detail(Long id) {
        AccessCustomerServer accessCustomerServer = accessCustomerServerMapper.selectById(id);
        CustomerServerVO customerServerVO = new CustomerServerVO();
        BeanUtils.copyProperties(accessCustomerServer, customerServerVO);
        return customerServerVO;
    }

    @Override
    public boolean insertOrUpdate(AccessCustomerServer accessCustomerServer) {
        return accessCustomerServerMapper.insertOrUpdate(accessCustomerServer);
    }

    @Override
    public int batchDelete(List<Long> ids) {
        return accessCustomerServerMapper.deleteByIds(ids);
    }

    @Override
    public List<CustomerServerVO> batchExport(CustomerServerQuery customerServerQuery, HttpServletResponse response) {
        List<AccessCustomerServer> customerServers;

        // 判断是否是全量导出
        if (!customerServerQuery.getIsAll()) {
            customerServers = accessCustomerServerMapper.selectByIds(customerServerQuery.getIds());
        } else {
            LambdaQueryWrapper<AccessCustomerServer> customerServerLambdaQueryWrapper = new LambdaQueryWrapper<>();
            buildLambdaQueryWrapper(customerServerQuery, customerServerLambdaQueryWrapper);
            customerServers = accessCustomerServerMapper.selectList(customerServerLambdaQueryWrapper);
        }

        return customerServers.stream().map(e -> {
            CustomerServerVO customerServerVO = new CustomerServerVO();
            BeanUtils.copyProperties(e, customerServerVO);
            return customerServerVO;
        }).toList();
    }

    @Override
    public int batchImport(MultipartFile file) throws IOException {
        // 提取EXCEL的数据
        List<CustomerServerVO> customerServerVOS = ExcelUtil.importExcel(file.getInputStream(), CustomerServerVO.class);
        if (CollectionUtils.isEmpty(customerServerVOS)) {
            log.info("数据为空");
            return 0;
        }

        List<AccessCustomerServer> customerManagements = customerServerVOS.stream().map(e -> {
            AccessCustomerServer accessCustomerServer = new AccessCustomerServer();
            BeanUtils.copyProperties(e, accessCustomerServer);
            return accessCustomerServer;
        }).toList();
        List<BatchResult> batchResults = accessCustomerServerMapper.insertOrUpdate(customerManagements);
        return batchResults.size();
    }

    private static void buildLambdaQueryWrapper(CustomerServerQuery customerServerQuery, LambdaQueryWrapper<AccessCustomerServer> accessCustomerServerLambdaQueryWrapper) {
        accessCustomerServerLambdaQueryWrapper.eq(StringUtils.isNotEmpty(customerServerQuery.getCustomer()), AccessCustomerServer::getCustomer, customerServerQuery.getCustomer());
        accessCustomerServerLambdaQueryWrapper.eq(StringUtils.isNotEmpty(customerServerQuery.getHandleResult()), AccessCustomerServer::getHandleResult, customerServerQuery.getHandleResult());
    }
}
